package com.example.news.provider;

import com.bumptech.glide.Glide;
import com.example.news.MainAbility;
import com.example.news.ResourceTable;
import com.example.news.bean.JiSuNews;
import com.example.news.fraction.HomeFraction;
import com.example.news.fraction.TouTiao;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

public class Newsprovider extends BaseItemProvider {
    private List<JiSuNews.ResultBean.ListBean> newsList;
    private Ability context;

    public Newsprovider(List<JiSuNews.ResultBean.ListBean> newsList, Ability context) {
        this.newsList = newsList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return newsList == null ? 0 : newsList.size();
    }

    @Override
    public Object getItem(int i) {
        if (newsList != null && i >= 0 && i < newsList.size()) {
            return newsList.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        ViewHolder viewHolder;
        final Component cpt;
        if (component == null) {
            //获取每一个item里面的布局对象
            cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_lists, null, false);
            cpt.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    Intent intent = new Intent();
                    Operation operation = new Intent.OperationBuilder()
                            .withDeviceId("")
                            .withBundleName("com.example.news.hmservice")
                            .withAbilityName("com.example.news.DetailAbility")
                            .build();
                    intent.setOperation(operation);
                    intent.setParam("news", newsList.get(i).getUrl());
                    context.startAbility(intent);
                }
            });
            viewHolder = new ViewHolder(cpt);
            cpt.setTag(viewHolder);
        } else {
            cpt = component;
            viewHolder = (ViewHolder) cpt.getTag();
        }

        //获取每一个item里面的数据
        JiSuNews.ResultBean.ListBean news = newsList.get(i);
        Glide.with(context).load(news.getPic()).into(viewHolder.pic);
        viewHolder.title.setText(news.getTitle());
        viewHolder.src.setText(news.getSrc());
        viewHolder.time.setText(news.getTime());
        return cpt;
    }

    private static class ViewHolder {
        Image pic;
        Text title;
        Text src;
        Text time;


        public ViewHolder(Component component) {
            pic = (Image) component.findComponentById(ResourceTable.Id_news_img);
            title = (Text) component.findComponentById(ResourceTable.Id_news_title);
            src = (Text) component.findComponentById(ResourceTable.Id_news_src);
            time = (Text) component.findComponentById(ResourceTable.Id_news_time);


        }
    }
}

